<?php

namespace app\ruinuo\controller;

use app\common\model\RuinuoButton;
use app\ruinuo\config\form\Lists as ThatLists;
use app\ruinuo\config\form\Logic as ThatLogic;
use app\common\model\RuinuoForm as DataMode;
use think\facade\Request;
use app\common\middleware\Auth;
use app\common\middleware\Power;
use app\common\middleware\Log;

class Form
{
    protected $middleware = [Auth::class => [], Power::class => [], Log::class => []];

    /*列表模板*/
    public function Index()
    {
        return ViewTpl('ruinuo/form/lists');
    }

    /*列表模板及数据*/
    public function Lists()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLists = new ThatLists();
            $data['rows'] = $ThatLists->Lists(input());
            $data['total'] = $ThatLists->Count(input());
            return BackCode($data);
        }
    }

    /*列表配置*/
    public function Tabs()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLists = new ThatLists();
            $data = $ThatLists->Tabs(input());
            return BackCode($data);
        }
    }

    /*明细*/
    public function Detail()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->Detail(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('ruinuo/form/form');
    }

    /*新增数据*/
    public function Add()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->Add(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('ruinuo/form/form');
    }

    /*修改数据*/
    public function Edit()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->Edit(input());
            if (!empty($data['data']['id'])) {
                $RuinuoButton = new RuinuoButton();
                $RuinuoForm = new DataMode();
                $Form = $RuinuoForm->where("id='" . $data['data']['id'] . "'")->field("mode_id")->find();
                if (!empty($Form['mode_id'])) {
                    $where = "mode_id='" . $Form['mode_id'] . "' and (action='add' or action='edit' or action='detail')";
                    $update = [
                        'width' => input("width"),
                        'height' => input("height"),
                    ];
                    $RuinuoButton->where($where)->update($update);
                }
            }
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('ruinuo/form/form');
    }

    /*删除数据*/
    public function Delete()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->Delete(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*批量删除数据*/
    public function AllDelete()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->Delete(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*单个字段修改*/
    public function UpField()
    {
        if (Request::isAjax() || Request::isPost()) {
            $ThatLogic = new ThatLogic();
            $data = $ThatLogic->UpdateField(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*导入Excel数据*/
    public function InExcel()
    {
        if (Request::isAjax() || Request::isPost()) {
            $file = Request::file("file") ?: Request::file("edit");
            $ThatLists = new ThatLists();
            $data = $ThatLists->InExcel($file);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*导出Excel数据*/
    public function OutExcel()
    {
        $ThatLists = new ThatLists();
        $ThatLists->OutExcel(input());
    }

    /*下载Excel模板*/
    public function TemplateExcel()
    {
        $ThatLists = new ThatLists();
        $ThatLists->TemplateExcel();
    }


}